package com.andnux.android;

import androidx.multidex.MultiDexApplication;

import com.andnux.android.data.SystemUsers;
import com.andnux.android.network.CacheUtils;
import com.andnux.android.network.GsonUtils;
import com.google.gson.Gson;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

public class MainApp extends MultiDexApplication {

    private static MainApp app;
    private final Gson gson = GsonUtils.gson;
    private final static String KEY_USERS = "users";

    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            layout.setPrimaryColorsId(R.color.white, R.color.black);//全局设置主题颜色
            return new ClassicsHeader(context);
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> {
            //指定为经典Footer，默认是 BallPulseFooter
            layout.setPrimaryColorsId(R.color.white, R.color.black);//全局设置主题颜色
            return new ClassicsFooter(context).setDrawableSize(20);
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
    }

    public SystemUsers getSystemUsers() {
        String users = CacheUtils.getString(KEY_USERS);
        return gson.fromJson(users, SystemUsers.class);
    }

    public void setSystemUsers(SystemUsers users) {
        String json = gson.toJson(users);
        CacheUtils.setString(KEY_USERS, json);
    }

    public static MainApp getApp() {
        return app;
    }
}
